starter.controller('homePageController', function($scope, $state, $ionicHistory, $ionicPopup, $ionicLoading,$http,loggedInUserService, $cordovaCamera, $cordovaFile, $cordovaFileTransfer, $cordovaDevice, $ionicPopup, $cordovaActionSheet, $ionicPlatform, $cordovaCamera) {
    $scope.response;
	var WINDOWS_BASE_URI = 'http://192.168.43.20:9090/spring-security-basic-auth/';
	var AWS_BASE_URI = 'http://ec2-13-126-78-43.ap-south-1.compute.amazonaws.com:5001/app/aws/';
	var USE_THIS_URI = AWS_BASE_URI;
    $("#uploadimage").on('submit',(function(e) {
        e.preventDefault();
        $("#message").empty();
        $('#loading').show();
        $.ajax({
        url: USE_THIS_URI + "uploadnew/", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: new FormData(this), // Data sent to server, a set of akey/value pairs (i.e. form fields and values)
		contentType: false,       // The content type used when sending data to the server.
        cache: false,             // To unable request pages to be cached
        processData:false,        // To send DOMDocument or non processed data file it is set to false
        headers: {
            'authorization': 'Basic Sm9objEyMzpwYXNzd29yZA=='
        },
        success: function(datar)   // A function to be called if request succeeds
        {
		var ff = new  FormData(this);
		console.log(new FormData(this));
		console.log('-----------------------');
        $('#loading').hide();
        datar="data:image/png;base64,"+ datar;
        $("#message").html('lode');
        console.log(datar);
        alert(datar);
		document.getElementById("previewing").src = datar;
		alert(datar+" SET");
        alert("Done");
		console.log('---------------');
		console.log(new FormData(this));
        },
		error : function (er) {
              console.log(er);console.log("er");
            }
        });
        }));


        $(function() {
            $("#file").change(function() {
                $("#message").empty(); // To remove the previous error message
                var file = this.files[0];
                var imagefile = file.type;
                var match= ["image/jpeg","image/png","image/jpg"];
                if(!((imagefile==match[0]) || (imagefile==match[1]) || (imagefile==match[2]))){
                    $('#previewing').attr('src','noimage.png');
                    $("#message").html("<p id='error'>Please Select A valid Image File</p>"+"<h4>Note</h4>"+"<span id='error_message'>Only jpeg, jpg and png Images type allowed</span>");
                    return false;
                } else {
                    var reader = new FileReader();
                    reader.onload = imageIsLoaded;
                    reader.readAsDataURL(this.files[0]);
                }
            });
            });
            function imageIsLoaded(e) {
                $("#file").css("color","green");
                $('#image_preview').css("display", "block");
                $('#previewing').attr('src', e.target.result);
                $('#previewing').attr('width', '250px');
                $('#previewing').attr('height', '230px');
            };
            $scope.image = null;
			$scope.foorm = new FormData();
		    $scope.foorm.append("files","jjjjjj");
            $scope.takepicture = function() {
				var options = {
            quality : 100,
            destinationType : 0,
            allowEdit : true,
            targetWidth: 1080,
            targetHeight: 1920,
            saveToPhotoAlbum: true
        };
			
              $cordovaCamera.getPicture(options)
                .then(function(data) {
				  data = "data:image/jpeg;base64," + data;
				  document.getElementById("previewing").src = data;
				  document.getElementById("captured").value = data;
				  data = "";
		  }, function(error) {
		  console.log(error);
		  alert(error+" saurabh");
		});
	}
	
	$scope.dloadfile = function () {
	   var uri = encodeURI("https://s14.postimg.cc/dmur2ldbj/bitcoin1.jpg");
	   var fileURL =  "///storage/emulated/0/DCIM/myFile";

	   $cordovaFileTransfer.download(
		  uri, fileURL, function(entry) {
			 console.log("download complete: " + entry.toURL());
		  },
			
		  function(error) {
			 console.log("download error source " + error.source);
			 console.log("download error target " + error.target);
			 console.log("download error code" + error.code);
		  },
			
		  false, {
			 headers: {
				"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
			 }
		  }
	   );
	}
	
	$scope.dloadfile3 = function () {
    //var url = "http://ec2-13-126-78-43.ap-south-1.compute.amazonaws.com:5001/Doctor_Template_03_25.jpg";
	var canvas = document.getElementById("canvas1");
	var url = canvas.toDataURL("image/jpg");
    var filename = "aaa.jpg";
    var targetPath = "///storage/emulated/0/DCIM/" + filename;
    alert(targetPath);

    $cordovaFileTransfer.download(url, targetPath, {}, true).then(function (result) {
        alert('Success!');
    }, function (error) {
        alert('Error!');
    }, function (progress) {
        // PROGRESS HANDLING GOES HERE
    });
  }
	
	$scope.dloadfile2 = function () {

    var url = "http://ec2-13-126-78-43.ap-south-1.compute.amazonaws.com:5001/Doctor_Template_03_25.jpg";
    var filename = url.split("/").pop();
    var targetPath = "///storage/emulated/0/DCIM/" + filename;
    alert(targetPath);

    $cordovaFileTransfer.download(url, targetPath, {}, true).then(function (result) {
        alert('Success!');
    }, function (error) {
        alert('Error!');
    }, function (progress) {
        // PROGRESS HANDLING GOES HERE
    });
  };
	
	
	
////////////////////////////
	
	$("#clicker").on('click',(function(e) {
        e.preventDefault();
		var form = document.forms.namedItem("uploadimagecamera");
		var dd = new FormData(form);
        $.ajax({
        url: USE_THIS_URI+"/uploadbytes/", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: dd, // Data sent to server, a set of akey/value pairs (i.e. form fields and values)
		contentType: false,       // The content type used when sending data to the server.
        cache: false,             // To unable request pages to be cached
        processData:false,        // To send DOMDocument or non processed data file it is set to false
        headers: {
            'authorization': 'Basic Sm9objEyMzpwYXNzd29yZA=='
        },
        success: function(datar)
        {
		$('#loading').hide();
        datar="data:image/png;base64,"+ datar;
        $("#message").html('lde');
        document.getElementById("previewing").src = datar;
		alert("Done");
		},
		error : function (er) {
              alert(er);
            }
        });
        }));
});